/**
 * 
 */
package com.qf.apps;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.multipart.MultipartFile;
import com.qf.apps.vo.UploadForm;

/**
 * @author 123
 * 
 */
public class UploadController extends SimpleFormController {
	private String path;

	public UploadController() {
		this.setCommandClass(UploadForm.class);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		UploadForm uploadForm = (UploadForm) command;
		MultipartFile file = uploadForm.getContents();
		String originalFileName = file.getOriginalFilename();
		String storedPath = path + System.getProperty("file.separator")
				+ originalFileName;
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
				new FileOutputStream(storedPath));
		bufferedOutputStream.write(file.getBytes());
		bufferedOutputStream.close();
		return new ModelAndView(getSuccessView());

	}

	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		System.out.println("initBinder");
		super.initBinder(request, binder);
		binder.registerCustomEditor(byte[].class,
				new ByteArrayMultipartFileEditor());

	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public static void main(String a[]) {
		String s = "sdfsdf\\sdfsd\\ccc";
		System.out.println(s.lastIndexOf("\\"));
		System.out.println(s.substring(s.lastIndexOf("\\\\")));
	}

}
